package com.eorchis.module.news.ui.controller;

import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.InputStream;
import java.io.OutputStream;
import java.lang.reflect.InvocationTargetException;
import java.util.ArrayList;
import java.util.List;
import javax.annotation.Resource;
import javax.servlet.ServletContext;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.commons.beanutils.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.context.WebApplicationContext;
import com.eorchis.api.IUser;
import com.eorchis.constants.DeptConstants;
import com.eorchis.constants.RoleConstants;
import com.eorchis.core.service.IBaseService;
import com.eorchis.core.springext.mvc.SessionConstant;
import com.eorchis.core.ui.commond.ResultState;
import com.eorchis.core.ui.commond.converter.IQueryCommondAttributeConverter;
import com.eorchis.core.ui.controller.AbstractBaseController;
import com.eorchis.module.blob.domain.BaseBlob;
import com.eorchis.module.blob.domain.BlobCondition;
import com.eorchis.module.blob.service.IBlobService;
import com.eorchis.module.blob.service.impl.BlobServiceImpl;
import com.eorchis.module.classuser.dao.IClassUserDao;
import com.eorchis.module.department.domain.Department;
import com.eorchis.module.news.domain.News;
import com.eorchis.module.news.domain.NewsQueryBean;
import com.eorchis.module.news.domain.ReceiveUserQueryBean;
import com.eorchis.module.news.service.INewsService;
import com.eorchis.module.news.ui.commond.NewsQueryCommond;
import com.eorchis.module.news.ui.commond.NewsValidCommond;
import com.eorchis.module.newsuserlink.service.INewsUserLinkService;
import com.eorchis.module.remind.domain.RemindUser;
import com.eorchis.module.role.domain.Role;
import com.eorchis.module.trainingclass.service.ITrainingClassService;
import com.eorchis.module.trainingclass.ui.commond.TrainingClassValidCommond;
import com.eorchis.module.user.domain.User;
import com.eorchis.utils.utils.PropertyUtil;

/**
 * Title: NewsController<br>
 * Description: 消息管理管理控制器<br>
 * Company: GoldGov<br>
 * Copyright @ 2012 GoldGov .All rights reserved.<br>
 * @author RongSL
 * @createDate 2016-05-06
 * @version $Revision$
 */
@Controller("NewsController")
@RequestMapping(value = NewsController.MODULE_PATH)
public class NewsController extends AbstractBaseController<NewsValidCommond,NewsQueryCommond> {
	// 模块页面根路径
	public static final String MODULE_PATH = "/module/news";
	// 消息管理接口
	@Resource(name = "com.eorchis.module.news.service.impl.NewsServiceImpl")
	private INewsService newsService;
	// 培训班信息接口
	@Autowired
	@Qualifier("com.eorchis.module.trainingclass.service.impl.TrainingClassServiceImpl")
	private ITrainingClassService trainingClassService;
	// 消息用户关联表接口
	@Resource(name = "com.eorchis.module.newsuserlink.service.impl.NewsUserLinkServiceImpl")
	private INewsUserLinkService newsUserLinkService;
	@Resource(name = "com.eorchis.module.classuser.dao.impl.ClassUserDaoImpl")
	private IClassUserDao classUserDao;
	//用于排序
	@Autowired
	@Qualifier("attributeConverter")
	private ArrayList<IQueryCommondAttributeConverter> attributeConverter;
	@Override
	public IBaseService getService() {
		return newsService;
	}
	@Override
	public String getModulePath() {
		return MODULE_PATH;
	}
	/**
	 * 查询未选接收人列表
	 * @param queryCommond
	 * @param request
	 * @param response
	 * @param resultState
	 * @return
	 * @throws Exception
	 * @author NingSS
	 * @createDate 2016-5-10
	 */
	@RequestMapping(value = "/findNoSelectedReceiveUserList")
	public String findNoSelectedReceiveUserList(
			@ModelAttribute("resultList") NewsQueryCommond qCommond,
			HttpServletRequest request, HttpServletResponse response,
			@ModelAttribute("resultState") ResultState resultState) throws Exception {
		if (attributeConverter != null) {
			parameterValueConver(qCommond, request, attributeConverter);
		}
		//执行查询逻辑 
		List<ReceiveUserQueryBean> resultList = newsService.findNoSelectedReceiveUserList(qCommond);
		qCommond.setResultList(resultList);
		resultState.setState(ResultState.RESULT_STATE_SUCCESS);
		return "";
	}
	/**
	 * 用于排序的方法
	 * @param queryCommond
	 * @param request
	 * @param attributeConverter
	 */
	@SuppressWarnings("unchecked")
	private void parameterValueConver(NewsQueryCommond queryCommond,HttpServletRequest request,
			List<IQueryCommondAttributeConverter> attributeConverter) {
		for (IQueryCommondAttributeConverter queryCommondAttributeConverter : attributeConverter) {
			queryCommondAttributeConverter.convert(queryCommond,
					request.getParameterMap());
		}
	}
	/**
	 * 查询已选接收人列表
	 * @param queryCommond
	 * @param request
	 * @param response
	 * @param resultState
	 * @return
	 * @throws Exception
	 * @author NingSS
	 * @createDate 2016-5-10
	 */
	@RequestMapping(value = "/findSelectedReceiveUserList")
	public String findSelectedReceiveUserList(
			@ModelAttribute("resultList") NewsQueryCommond qCommond,
			HttpServletRequest request, HttpServletResponse response,
			@ModelAttribute("resultState") ResultState resultState) throws Exception {
		if (attributeConverter != null) {
			parameterValueConver(qCommond, request, attributeConverter);
		}
		//执行查询逻辑 
		List<ReceiveUserQueryBean> resultList = newsService.findSelectedReceiveUserList(qCommond);
		qCommond.setResultList(resultList);
		resultState.setState(ResultState.RESULT_STATE_SUCCESS);
		return "";
	}
	/**
	 * 上传附件
	 * @param commond
	 * @param request
	 * @param response
	 * @param resultState
	 * @return
	 * @throws Exception
	 * @author NingSS
	 * @date 2016-5-11
	 */
	@RequestMapping("/uploadNewsFile")
	public String uploadNewsFile(@ModelAttribute("data")NewsValidCommond commond,
			HttpServletRequest request,HttpServletResponse response,
			@ModelAttribute("resultState") ResultState resultState)
			throws Exception {	
		//新增附件
		newsService.uploadNewsFile(commond,request);
		resultState.setState(ResultState.RESULT_STATE_SUCCESS);
		try {
		}catch (Exception e) {
			resultState.setMessage("上传失败!");
			resultState.setState(ResultState.RESULT_STATE_FAIL);
		}
		return "";
	}
	/**
	 * 新增消息
	 * @param commond
	 * @param request
	 * @param response
	 * @param resultState
	 * @return
	 * @throws Exception
	 * @author NingSS
	 * @date 2016-5-11
	 */
	@RequestMapping("/addNews")
	public String addNews(@ModelAttribute("data")NewsValidCommond commond,
			HttpServletRequest request,HttpServletResponse response,
			@ModelAttribute("resultState") ResultState resultState)
			throws Exception {	
		User user = (User) request.getSession().getAttribute(SessionConstant.USER_INFO);
//		Department d = (Department) request.getSession().getAttribute(SessionConstant.CURRENT_SCOPE);
		Department d= classUserDao.find(Department.class, DeptConstants.XCH_DEPT_ID);
		try{
			Integer.valueOf(user.getUserId());
		}catch(Exception e){
			e.printStackTrace();
			resultState.setMessage("当前登录信息异常，请重新登录!");
			resultState.setState(ResultState.RESULT_STATE_FAIL);
			return "";
		}
		try{
			if(PropertyUtil.objectNotEmpty(user)&&PropertyUtil.objectNotEmpty(user.getUserId())){
				//新增附件
				newsService.uploadNewsFile(commond,request);
				
				if(PropertyUtil.objectNotEmpty(commond.isFlag())&&false==commond.isFlag()){
					resultState.setMessage(commond.getMessage());
					resultState.setState(ResultState.RESULT_STATE_FAIL);
				}else{
					commond.setSendUserId(user.getUserId());//消发送人ID
					commond.setTreepath(d.getTreepath());
					//保存消息表数据
					newsService.addNews(commond);
					if(!PropertyUtil.objectNotEmpty(commond.getReceiveUserName())){
						//批量发送消息 ，发送对象：自己部门下的所有人
						newsUserLinkService.batchSaveNewsUserLink(commond);
					}
					resultState.setState(ResultState.RESULT_STATE_SUCCESS);
				}
			}else{
				resultState.setMessage("缺少参数,上传失败!");
				resultState.setState(ResultState.RESULT_STATE_FAIL);
			}
		}catch(Exception e){
			resultState.setMessage(commond.getMessage());
			resultState.setState(ResultState.RESULT_STATE_FAIL);
		}
		return "";
	}
	/**
	 * 下载附件
	 * @param commond
	 * @param request
	 * @param response
	 * @param resultState
	 * @return
	 * @throws Exception
	 * @author NingSS
	 * @date 2016-5-11
	 */
	@RequestMapping("/downloadNewsFile")
	public void downloadNewsFile(
			@ModelAttribute("resultList") NewsValidCommond commond,
			HttpServletRequest request, HttpServletResponse response,
			@ModelAttribute("resultState") ResultState resultState)
			throws Exception {
		ServletContext sc = request.getSession().getServletContext();
		WebApplicationContext ctx = (WebApplicationContext)sc.getAttribute("org.springframework.web.context.WebApplicationContext.ROOT");
		IBlobService service=(BlobServiceImpl)ctx.getBean("com.eorchis.module.blob.service.impl.BlobServiceImpl");
		String blobId=request.getParameter("sn");
		List <ReceiveUserQueryBean> blobList=newsService.findBlobList(blobId);
		if(!PropertyUtil.objectNotEmpty(blobList) && blobList.size()<1){
			resultState.setMessage("附件不存在");
		}else{
		BlobCondition condition = new BlobCondition();
		condition.setBlobId(blobId);
		int BUFFERSIZE = 102400;
		OutputStream writer = null;
		BufferedOutputStream bufferWriter = null;
		InputStream reader = null;
		BufferedInputStream bufferReader = null;
		try {
			BaseBlob blob = service.getBlob(condition);
			writer = response.getOutputStream();
			bufferWriter = new BufferedOutputStream(writer);
			reader = blob.getAttBlob().getBinaryStream();
			bufferReader = new BufferedInputStream(reader);

			//response.setContentType(request.getSession().getServletContext().getMimeType("xlms"));
			//response.setContentType(sc.getMimeType("text"));
			//
			//request.setCharacterEncoding("UTF-8");
			String name =request.getParameter("name");
			
			if(!PropertyUtil.objectNotEmpty(name)){
				name="未命名";
			}
			String fileName=null;
			//fileName = URLEncoder.encode(name, "UTF-8");
			fileName = new String(name.getBytes("gb2312"), "ISO-8859-1");
			response.setContentType(request.getSession().getServletContext().getMimeType(fileName));
			response.setHeader("Content-Disposition", "attachment;filename=\"" + fileName); 
			int i = 0;
			
			byte bytes[] = new byte[BUFFERSIZE];
			while((i = bufferReader.read(bytes)) != -1) {
				bufferWriter.write(bytes, 0, i);
			}
			bufferWriter.flush(); 
		}catch(Exception e){
			e.printStackTrace();
		}finally {
			bufferWriter.close();
			bufferReader.close();
			writer.close();
			reader.close();
		}
	   }
	}
	
	/**
	 * 查看单个消息
	 * @param commond
	 * @param request
	 * @param response
	 * @param resultState
	 * @return
	 * @throws Exception
	 * @author NingSS
	 * @date 2016-5-11
	 */
	@RequestMapping("/findNews")
	public String findNews(@ModelAttribute("data")NewsValidCommond commond,
			HttpServletRequest request,HttpServletResponse response,
			@ModelAttribute("resultState") ResultState resultState)
			throws Exception {	
		resultState.setState(ResultState.RESULT_STATE_SUCCESS);
		if(PropertyUtil.objectNotEmpty(commond.getNewsId())){
			NewsValidCommond result = newsService.find(commond.getNewsId());
			if (PropertyUtil.objectNotEmpty(result)) {
				NewsValidCommond idsAndNames = newsService.findReceiveUser(result);
				result.setReceiveUserIds(idsAndNames.getReceiveUserIds());
				result.setReceiveUserNames(idsAndNames.getReceiveUserNames());
				result.setAttachName(idsAndNames.getAttachName());
				result.setAttachType(idsAndNames.getAttachType());
				org.springframework.beans.BeanUtils.copyProperties(result, commond);
			}
		}else{
			resultState.setMessage("请勾选想要查看的消息!");
			resultState.setState(ResultState.RESULT_STATE_FAIL);
		}
		return "";
	}

	/**
	 * 查询消息列表
	 * @param queryCommond
	 * @return
	 * @throws Exception
	 * @author LongJZ
	 * @createDate 2016-6-21
	 */
	@RequestMapping(value = "/findNewsList")
	public String findNewsList(@ModelAttribute("resultList") NewsQueryCommond queryCommond,
			HttpServletRequest request, HttpServletResponse response,
			@ModelAttribute("resultState") ResultState resultState) throws Exception {
		resultState.setState(ResultState.RESULT_STATE_FAIL);
		User user = (User) request.getSession().getAttribute(SessionConstant.USER_INFO);
		Department d = (Department) request.getSession().getAttribute(SessionConstant.CURRENT_SCOPE);
		if (attributeConverter != null)
			parameterValueConver(queryCommond, request, attributeConverter);
		queryCommond.setSearchUserId(user.getID());
		// 执行查询逻辑 
		Role currentrole = (Role) request.getSession().getAttribute(SessionConstant.CURRENT_ROLE);
		//一级管理员查看全部通知，二级、三级管理员则只查询自己单位下的通知，学员查看发送给自己的通知
		if(PropertyUtil.objectNotEmpty(currentrole)){
			if(currentrole.getRoleCode().equals(RoleConstants.ROLE_CODE_ONE_ADMIN)){
				queryCommond.setRoleCode(currentrole.getRoleCode());
				queryCommond.setSearchUserId(user.getID());
			}else{
				queryCommond.setDeptID(d.getDeptID());
			}
			List<NewsQueryBean> resultList = newsService.findNewsList(queryCommond);
			queryCommond.setResultList(resultList);
			resultState.setMessage("查询成功！");
			resultState.setState(ResultState.RESULT_STATE_SUCCESS);
		}else{
			resultState.setMessage("您没有权限查看！");
		}
		return "";
	}
	
	/**
	 * 查询培训班相关管理员
	 * @Date 2016-06-15
	 * @author RongSL
	 * */
	@RequestMapping("findClassAdministrator")
	public String findClassAdministrator(@ModelAttribute("resultList") NewsQueryCommond qCommond,HttpServletRequest request, HttpServletResponse response,@ModelAttribute("resultState") ResultState resultState){
		ResultState rs=new ResultState();
		try {
			User user = (User) request.getSession().getAttribute(SessionConstant.USER_INFO);
			qCommond.setSearchUserId(user.getUserId());
			List<RemindUser> list=newsService.findAdministrator(qCommond);
			qCommond.setResultList(list);
			rs.setMessage("查询成功!");
			rs.setState(ResultState.RESULT_STATE_SUCCESS);
		} catch (Exception e) {
			e.printStackTrace();
			rs.setMessage("查询管理员失败!");
			rs.setState(ResultState.RESULT_STATE_FAIL);
		}finally{
			try {
				BeanUtils.copyProperties(rs, resultState);
			} catch (Exception e) {
			}
		}
		return "";
	}

	@RequestMapping("findDefaultValue")
	public String findDefaultValue(@ModelAttribute("result") NewsValidCommond validCommond,HttpServletRequest request,HttpServletResponse response,@ModelAttribute("resultState") ResultState resultState){
		ResultState rs=new ResultState();
		if(PropertyUtil.objectNotEmpty(validCommond.getSearchClassID())){
			try {
				TrainingClassValidCommond Bean=trainingClassService.find(validCommond.getSearchClassID());
				request.getSession().getAttributeNames();
				validCommond.setDefaultValue("―以上信息来自"+Bean.getClassName()+"。");
				rs.setMessage("查询成功！");
				rs.setState(ResultState.RESULT_STATE_SUCCESS);
			}catch (Exception e) {
				rs.setMessage("查询失败!");
				rs.setState(ResultState.RESULT_STATE_FAIL);
			}
		}else{
			rs.setMessage("请勾选想要查看的消息!");
			rs.setState(ResultState.RESULT_STATE_FAIL);
		}
		try {
			BeanUtils.copyProperties(rs, resultState);
		} catch (Exception e) {
		}
		return "";
	}

	/**
	 * 发送消息列表 - 删除消息
	 * @return
	 * @throws Exception
	 * @author LongJZ
	 * @date 2016-6-22
	 */
	@RequestMapping("/deleteNews")
	public String deleteNews(@ModelAttribute("data") NewsValidCommond commond, HttpServletRequest request,
			HttpServletResponse response, @ModelAttribute("resultState") ResultState resultState) throws Exception {
		if (PropertyUtil.objectNotEmpty(commond.getNewsIds()) && commond.getNewsIds().length > 0) {
			try {
				for (String newsId : commond.getNewsIds()) {
					NewsValidCommond news = new NewsValidCommond();
					news = newsService.find(newsId);
					news.setSendLookState(News.IS_N);
					newsService.update(news);
				}
			} catch (Exception e) {
				resultState.setMessage("删除失败!");
				resultState.setState(ResultState.RESULT_STATE_FAIL);
			}
		} else {
			resultState.setMessage("缺少参数!");
			resultState.setState(ResultState.RESULT_STATE_FAIL);
		}
		return "";
	}
	
	
	/**
	 *  @throws InvocationTargetException 
	 * @throws IllegalAccessException 
	 * @接收未读消息数量
	  * @Name: receiveNewsCount
	  * @Description: @param validCommond
	  * @Description: @param request
	  * @Description: @param response
	  * @Description: @param resultState
	  * @Description: @return
	  * @Author: GUOLEI
	  * @Version: V1.00
	  * @Create Date: 2016-6-23下午5:04:13
	  * @Parameters: NewsController
	  * @Return: String
	 */
	@RequestMapping("receiveNewsCount")
	public String receiveNewsCount(@ModelAttribute("result") NewsValidCommond validCommond,HttpServletRequest request,HttpServletResponse response,@ModelAttribute("resultState") ResultState resultState) throws IllegalAccessException, InvocationTargetException{
		ResultState rs=new ResultState();
		Integer r=0;
		try {
			r=newsService.receiveNewsCount(validCommond);
			validCommond.setReceiveNum(r);
			rs.setMessage("查询成功！");
			rs.setState(ResultState.RESULT_STATE_SUCCESS);
		}catch (Exception e) {
			rs.setMessage("查询失败!");
			rs.setState(ResultState.RESULT_STATE_FAIL);
		}
		BeanUtils.copyProperties(rs, resultState);
		return "";
	}
	
	
	/**
	  * @throws Exception 
	  * @接收消息列表
	  * @Name: replyNewsUserList
	  * @Description: @param validCommond
	  * @Description: @param request
	  * @Description: @param response
	  * @Description: @param resultState
	  * @Description: @return
	  * @Author: GUOLEI
	  * @Version: V1.00
	  * @Create Date: 2016-6-23下午5:04:50
	  * @Parameters: NewsController
	  * @Return: String
	 */
	@RequestMapping("replyNewsUserList")
	public String replyNewsUserList(@ModelAttribute("resultList") NewsQueryCommond queryCommond,HttpServletRequest request,HttpServletResponse response,@ModelAttribute("resultState") ResultState resultState) throws Exception{
		resultState.setState(ResultState.RESULT_STATE_FAIL);
		// 执行查询逻辑 
		Role currentrole = (Role) request.getSession().getAttribute(SessionConstant.CURRENT_ROLE);
		User user = (User)request.getSession().getAttribute(SessionConstant.USER_INFO);
		queryCommond.setSearchUserId(user.getUserId());
		//一级管理员查看全部通知，二级、三级管理员则只查询自己单位下的通知，学员查看发送给自己的通知
		if(PropertyUtil.objectNotEmpty(currentrole)){
			List<NewsQueryBean> resultList = newsService.replyNewsUserList(queryCommond);
			queryCommond.setResultList(resultList);
			resultState.setState(ResultState.RESULT_STATE_SUCCESS);
		}else{
			resultState.setMessage("请刷新页面后继续查看。");
			resultState.setState(ResultState.RESULT_STATE_FAIL);
		}
		return "";
	}
	
	/**
	  * @查询消息回复人员列表
	  * @Name: findReplyUserList
	  * @Description: @param queryCommond
	  * @Description: @param request
	  * @Description: @param response
	  * @Description: @param resultState
	  * @Description: @return
	  * @Description: @throws Exception
	  * @Author: GUOLEI
	  * @Version: V1.00
	  * @Create Date: 2016-6-23下午5:11:15
	  * @Parameters: NewsController
	  * @Return: String
	 */
	@RequestMapping("findReplyUserList")
	public String findReplyUserList(@ModelAttribute("resultList") NewsQueryCommond queryCommond,HttpServletRequest request,HttpServletResponse response,@ModelAttribute("resultState") ResultState resultState) throws Exception{
		IUser user = (IUser)request.getSession().getAttribute(SessionConstant.USER_INFO);
		queryCommond.setSearchUserId(user.getID());
		// 执行查询逻辑 
		List<NewsQueryBean> resultList = newsService.findReplyUserList(queryCommond);
		queryCommond.setResultList(resultList);
		resultState.setState(ResultState.RESULT_STATE_SUCCESS);
		return "";
	}
	
	
	/**
	  * @throws Exception 
	  * @回复消息列表
	  * @Name: findReplyNewsList
	  * @Description: @param validCommond
	  * @Description: @param request
	  * @Description: @param response
	  * @Description: @param resultState
	  * @Description: @return
	  * @Author: GUOLEI
	  * @Version: V1.00
	  * @Create Date: 2016-6-23下午5:11:49
	  * @Parameters: NewsController
	  * @Return: String
	 */
	@RequestMapping("findReplyNewsList")
	public String findReplyNewsList(@ModelAttribute("resultList") NewsQueryCommond queryCommond,HttpServletRequest request,HttpServletResponse response,@ModelAttribute("resultState") ResultState resultState) throws Exception{
		// 执行查询逻辑 
		IUser user = (IUser)request.getSession().getAttribute(SessionConstant.USER_INFO);
		queryCommond.setSearchUserId(user.getID());
		List<NewsQueryBean> resultList = newsService.findReplyNewsList(queryCommond);
		newsService.updateReplyReadStata(queryCommond);
		queryCommond.setResultList(resultList);
		resultState.setState(ResultState.RESULT_STATE_SUCCESS);
		return "";
	}
	
	
	/**
	  * @throws InvocationTargetException 
	  * @throws IllegalAccessException 
	  * @回复消息列表
	  * @Name: receiveNewsCount
	  * @Description: @param validCommond
	  * @Description: @param request
	  * @Description: @param response
	  * @Description: @param resultState
	  * @Description: @return
	  * @Author: GUOLEI
	  * @Version: V1.00
	  * @Create Date: 2016-6-23下午5:04:13
	  * @Parameters: NewsController
	  * @Return: String
	 */
	@RequestMapping("findReplyNews")
	public String findReplyNews(@ModelAttribute("result") NewsValidCommond validCommond,HttpServletRequest request,HttpServletResponse response,@ModelAttribute("resultState") ResultState resultState) throws IllegalAccessException, InvocationTargetException{
		ResultState rs=new ResultState();
		NewsQueryBean r=null;
		try {
			IUser user = (IUser)request.getSession().getAttribute(SessionConstant.USER_INFO);
			validCommond.setUserId(user.getID());
			validCommond.setReadState(News.IS_Y);
			r=newsService.findReplyNews(validCommond);
			if(PropertyUtil.objectNotEmpty(r)){				
				validCommond.setUserName(r.getUserName());
				validCommond.setNewsValue(r.getNewsValue());
				validCommond.setNewsCode(r.getNewsType());
				validCommond.setAttachName(r.getAttachName());
				validCommond.setAttachType(r.getAttachType());
				validCommond.setFileSize(r.getFileSize());
				validCommond.setAttachmentId(r.getAttachmentId());
				validCommond.setNewsTitle(r.getNewsTitle());
				validCommond.setClassName(r.getClassName());
				validCommond.setNewsTypeCode(r.getNewsCode());
				validCommond.setClassName(r.getClassName());
				validCommond.setReceiveTime(r.getReceiveTime());
				validCommond.setReceiveUserNames(user.getName());
			}
			newsService.updateReadState(validCommond);
			rs.setMessage("查询成功！");
			rs.setState(ResultState.RESULT_STATE_SUCCESS);
		}catch (Exception e) {
			e.printStackTrace();
			rs.setMessage("查询失败!");
			rs.setState(ResultState.RESULT_STATE_FAIL);
		}
		BeanUtils.copyProperties(rs, resultState);
		return "";
	}
	
	
	
	
	/**
	  * @throws InvocationTargetException 
	  * @throws IllegalAccessException 
	  * @回复消息列表
	  * @Name: receiveNewsCount
	  * @Description: @param validCommond
	  * @Description: @param request
	  * @Description: @param response
	  * @Description: @param resultState
	  * @Description: @return
	  * @Author: GUOLEI
	  * @Version: V1.00
	  * @Create Date: 2016-6-23下午5:04:13
	  * @Parameters: NewsController
	  * @Return: String
	 */
	@RequestMapping("updateReceiveLookStata")
	public String updateReceiveLookStata(@ModelAttribute("result") NewsValidCommond validCommond,HttpServletRequest request,HttpServletResponse response,@ModelAttribute("resultState") ResultState resultState) throws IllegalAccessException, InvocationTargetException{
		ResultState rs=new ResultState();
		try {
			IUser user = (IUser)request.getSession().getAttribute(SessionConstant.USER_INFO);
			validCommond.setUserId(user.getID());
			validCommond.setReplyState(News.IS_Y);
			String [] newsIds=  validCommond.getNewsIds();
			if(PropertyUtil.objectNotEmpty(newsIds)){
				for(String newsUserLink:newsIds){
					validCommond.setNewsUserLink(newsUserLink);
					newsService.updateReceiveLookStata(validCommond);
				}
			}
			rs.setMessage("修改成功！");
			rs.setState(ResultState.RESULT_STATE_SUCCESS);
		}catch (Exception e) {
			rs.setMessage("修改失败!");
			rs.setState(ResultState.RESULT_STATE_FAIL);
		}
		BeanUtils.copyProperties(rs, resultState);
		return "";
	}
	
	/**
	  * @throws InvocationTargetException 
	  * @throws IllegalAccessException 
	  * @回复消息列表
	  * @Name: receiveNewsCount
	  * @Description: @param validCommond
	  * @Description: @param request
	  * @Description: @param response
	  * @Description: @param resultState
	  * @Description: @return
	  * @Author: GUOLEI
	  * @Version: V1.00
	  * @Create Date: 2016-6-23下午5:04:13
	  * @Parameters: NewsController
	  * @Return: String
	 */
	@RequestMapping("addReply")
	public String addReply(@ModelAttribute("result") NewsValidCommond validCommond,HttpServletRequest request,HttpServletResponse response,@ModelAttribute("resultState") ResultState resultState) throws IllegalAccessException, InvocationTargetException{
		ResultState rs=new ResultState();
		IUser user = (IUser)request.getSession().getAttribute(SessionConstant.USER_INFO);
		try {
			validCommond.setUserId(user.getID());
			validCommond.setReplyState(News.IS_Y);
			validCommond.setLookState(News.IS_Y);
			validCommond.setReadState(News.IS_N);
			newsService.addReply(validCommond);
			rs.setMessage("添加成功！");
			rs.setState(ResultState.RESULT_STATE_SUCCESS);
		}catch (Exception e) {
			rs.setMessage("添加失败!");
			rs.setState(ResultState.RESULT_STATE_FAIL);
		}
		BeanUtils.copyProperties(rs, resultState);
		return "";
	}
}
